﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using E_Learning_V3.Models;
using E_Learning_V3.DAL;

namespace ElearningOUHv2.Controllers
{
    public class AfdelingController : Controller
    {
        private ElearningContext db = new ElearningContext();

        //
        // GET: /Afdeling/

        public ViewResult Index()
        {
            return View(db.Afdeling.ToList());
        }

        //
        // GET: /Afdeling/Details/5

        public ViewResult Details(int id)
        {
            Afdeling afdeling = db.Afdeling.Find(id);
            return View(afdeling);
        }

        //
        // GET: /Afdeling/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Afdeling/Create

        [HttpPost]
        public ActionResult Create(Afdeling afdeling)
        {
            if (ModelState.IsValid)
            {
                db.Afdeling.Add(afdeling);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(afdeling);
        }

        //
        // GET: /Afdeling/Edit/5

        public ActionResult Edit(int id)
        {
            ViewBag.emner = db.Emner;

            Afdeling afdeling = db.Afdeling.Find(id);
            if (afdeling.brugere == null)
            {
                afdeling.brugere = new List<Guid>();
            }
            if (afdeling.emner == null)
            {
                afdeling.emner = new List<int>();
            }
            Session["afdeling"] = afdeling;
            return View(afdeling);
        }

        //
        // POST: /Afdeling/Edit/5

        [HttpPost]
        public ActionResult Edit(Afdeling afdeling)
        {
            if (ModelState.IsValid)
            {
                db.Entry(afdeling).State = EntityState.Modified;
                db.SaveChanges();
                Session.Remove("afdeling");
                return RedirectToAction("Index");
            }

            return View(afdeling);
        }

        //
        // GET: /Afdeling/Delete/5

        public ActionResult Delete(int id)
        {
            Afdeling afdeling = db.Afdeling.Find(id);
            return View(afdeling);
        }

        //
        // POST: /Afdeling/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Afdeling afdeling = db.Afdeling.Find(id);
            db.Afdeling.Remove(afdeling);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        // Tilføjer emne til afdeling
        public PartialViewResult emnerInAfd()
        {
            ViewBag.emner = db.Emner;
            Afdeling afd = Session["afdeling"] as Afdeling;
            return PartialView(afd);
        }


        public PartialViewResult SaveEmnerInAfd(FormCollection fc)
        {
            foreach (string v in fc)
            {
                object lol = fc[v];
            }
            return PartialView("AfdOversigtPartial");    
        }
    }
}